package com.kjtpay.design_pattern.composite;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.kjtpay.design_pattern.R;
import com.kjtpay.design_pattern.memento.MementoActivity;

public class CompositeActivity extends AppCompatActivity {
    public static void launch(Context context) {
        if (null != context) {
            Intent intent = new Intent();
            intent.setClass(context, CompositeActivity.class);
            if (!(context instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(intent);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_composite);
        setTitle("组合模式");

        TextView treeTextView = findViewById(R.id.textViewTree);
        TextView dutyTextView = findViewById(R.id.textViewDuty);

        ConcreteCompany root = new ConcreteCompany("北京总公司");
        root.add(new HRDepartment("总公司人力资源部"));
        root.add(new FinanceDepartment("总公司财务部"));

        ConcreteCompany company = new ConcreteCompany("上海华东分公司");
        company.add(new HRDepartment("华东分公司人力资源部"));
        company.add(new FinanceDepartment("华东分公司财务部"));
        root.add(company);

        ConcreteCompany nanjing = new ConcreteCompany("南京办事处");
        nanjing.add(new HRDepartment("南京办事处人力资源部"));
        nanjing.add(new FinanceDepartment("南京办事处财务部"));
        company.add(nanjing);

        ConcreteCompany hangzhou = new ConcreteCompany("杭州办事处");
        hangzhou.add(new HRDepartment("杭州办事处人力资源部"));
        hangzhou.add(new FinanceDepartment("杭州办事处财务部"));
        company.add(hangzhou);

        String tree = "结构图：\n" + root.display(1);
        treeTextView.setText(tree);

        String duty = "职责：\n" + root.lineOfDuty();
        dutyTextView.setText(duty);
    }
}